/** 
*
* @author  jiwf 
* @date 创建时间：2016年10月17日 下午7:41:13 
* @version 1.0 
* @since  
*  
*/
package com.xbstar.tools;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Stream;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;

public class BeanUtils {
	public static String toUpperCaseFirstOne(String name){
		String a1=name.substring(0,1);
		name=a1.toUpperCase()+name.substring(1);
		return name;
	}
	/**
	 * 有from复制到to（from和to同时有一个字段，则以from为准）
	 * @param to 目的对象
	 * @param from 源对象
	 * @throws Exception
	 */
	public static <M> M merge(M to, M from) throws Exception {
		M o1=to;
		M o2=from;
		 Field[] field = o1.getClass().getDeclaredFields();
	        Method[] mm = o1.getClass().getDeclaredMethods();
	        for(int j=0 ; j<field.length ; j++){
	            String name = field[j].getName();    
	            String type = field[j].getGenericType().toString();    
	            if(type.equals("class java.lang.String")){   
	                Method m = o1.getClass().getMethod("get"+toUpperCaseFirstOne(name));
	                String value = (String) m.invoke(o1);
	                String value2 = (String) m.invoke(o2);
	                if(value2!=null){
	                    m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), String.class);
	                    m.invoke(o1, value2);
	                }
	            }else if("int".equals(type)) {
	                Method m = o1.getClass().getMethod("get"+toUpperCaseFirstOne(name));
	                int value = (Integer) m.invoke(o1);    
	                int value2 = (Integer) m.invoke(o2);   
	                if(value2!=0){
	                    m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), int.class);
	                    m.invoke(o1, value2);
	                }
	            }else if("class java.lang.Integer".equals(type)) {
	                Method m = o1.getClass().getMethod("get"+toUpperCaseFirstOne(name));
	                Integer value = (Integer) m.invoke(o1);    
	                Integer value2 = (Integer) m.invoke(o2);   
	                if(value2!=0){
	                    m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), Integer.class);
	                    m.invoke(o1, value2);
	                }
	            }
	            else if("java.lang.Long".equals(type)) {
	                Method m = o1.getClass().getMethod("get"+toUpperCaseFirstOne(name));
	                Long value = (Long) m.invoke(o1);    
	                Long value2 = (Long) m.invoke(o2);   
	                if(value2 != null&&value2!=0){
	                    m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), Long.class);
	                    m.invoke(o1, value2);
	                }
	            }
	            else if("class java.lang.Long".equals(type)) {
	                Method m = o1.getClass().getMethod("get"+toUpperCaseFirstOne(name));
	                Long value = (Long) m.invoke(o1);    
	                Long value2 = (Long) m.invoke(o2);   
	                if(value2 != null&&value2!=0){
	                    m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), Long.class);
	                    m.invoke(o1, value2);
	                }
	            }
	            else if("long".equals(type)) {
	                Method m = o1.getClass().getMethod("get"+toUpperCaseFirstOne(name));
	                Long value = (Long) m.invoke(o1);    
	                Long value2 = (Long) m.invoke(o2);   
	                if(value2 != null&&value2!=0){
	                    m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), long.class);
	                    m.invoke(o1, value2);
	                }
	            }else if("class java.lang.Double".equals(type)) {
	                Method m = o1.getClass().getMethod("get"+toUpperCaseFirstOne(name));
	                Double value = (Double) m.invoke(o1);    
	                Double value2 = (Double) m.invoke(o2);   
	                if(value2 != null&&value2!=0){
	                    m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), Double.class);
	                    m.invoke(o1, value2);
	                }
	            }
	            else{
	            	throw new Exception("没有格式化的对象属性");
	            }
	        }
		return o1;
	}
	/**
	 * 把httprequest的对象属性拷贝到对象上
	 * @param to
	 * @param req
	 * @return
	 */
	public static <M> M copyRequestToObj(M to, HttpServletRequest req) throws Exception{
		M o1=to;
		Field[] field = o1.getClass().getDeclaredFields();
        Method[] mm = o1.getClass().getDeclaredMethods();
        for(int j=0 ; j<field.length ; j++){
            String name = field[j].getName();    
            String type = field[j].getGenericType().toString();  
            String value=req.getParameter(name);
            if(value!=null&&!value.equals("")){
            	if(type.equals("class java.lang.String")){ 
            		 Method m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), String.class);
            		 m.invoke(o1, value);
            	}else if("int".equals(type)) { 
	                Method m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), int.class);
	                int va1=Integer.parseInt(value);
           		 	m.invoke(o1, va1);
	            }
            	else if("class java.lang.Integer".equals(type)) { 
	                Method m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), Integer.class);
	                Integer va1=Integer.parseInt(value);
           		 	m.invoke(o1, va1);
	            }else if("class java.lang.Long".equals(type)) { 
	                Method m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), Long.class);
	                Long va1=Long.parseLong(value);
           		 	m.invoke(o1, va1);
	            }else if("long".equals(type)) { 
	                Method m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), long.class);
	                long va1=Long.parseLong(value);
           		 	m.invoke(o1, va1);
	            }else if("class java.lang.Double".equals(type)) { 
	                Method m = o1.getClass().getMethod("set"+toUpperCaseFirstOne(name), Double.class);
	                Double va1=Double.parseDouble(value);
           		 	m.invoke(o1, va1);
	            }
	            else{
	            	throw new Exception("没有实现反射得到类型");
	            }
            }
        }
		return o1;
	}
	/**
	 * 从一个对象中拷贝出所需字段的值到json中
	 * @param obj 需要拷贝成json的对象
	 * @param attrs 需要拷贝的属性
	 * @return 拷贝出的json对象
	 */
	public static <M> JSONObject getAJsonFromObj(M obj,String[]attrs){
		JSONObject json=new JSONObject();
		if(attrs==null||attrs.length<1){
			Field[] field = obj.getClass().getDeclaredFields();
			 for(int j=0 ; j<field.length ; j++){
				 String name = field[j].getName();    
				 Method m;
				try {
					m = obj.getClass().getMethod("get"+toUpperCaseFirstOne(name));
					Object obwj=m.invoke(obj);
					if(obwj==null) continue;
					 String value = String.valueOf( obwj);
					 json.put(name, value);
				} catch (Exception e) {
					e.printStackTrace();
				} 
	               
			 }
		}else{
			try {
				Stream<String> words = Arrays.stream(attrs);
				 words.forEach(atrr->{
					 Method m;
					try {
						m = obj.getClass().getMethod("get"+toUpperCaseFirstOne(atrr));
						String value = String.valueOf( m.invoke(obj));
						json.put(atrr, value);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				 });
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		 
		return json;
	}
}
